from django.contrib import admin
from .models import Question, Choice

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3  # 后台默认显示3个选项字段

class QuestionAdmin(admin.ModelAdmin):
    # 字段分组显示
    fieldsets = [
        ("投票主题", {"fields": ["question_text"]}),
        ("发布时间", {"fields": ["pub_date"]}),
    ]
    # 关联选项内联显示
    inlines = [ChoiceInline]
    # 添加侧边栏过滤（重点功能）
    list_filter = ["pub_date"]  # 按发布时间过滤
    # 列表页显示字段（可选）
    list_display = ["question_text", "pub_date", "was_published_recently"]

# 注册模型及管理类
admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)  # 可选：若需要单独管理Choice模型